﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using Microsoft.Practices.Unity;

namespace Hopscotch.Common.Services.Wcf
{
    public class UnityInstanceProvider : IInstanceProvider
    {
        private readonly IUnityContainer container;
        public Type ServiceType { get; set; }

        public UnityInstanceProvider(IUnityContainer unityContainer)
        {
            container = unityContainer;
        }

        // Get Service instace via unity container
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return container.Resolve(ServiceType);
        }

        public object GetInstance(System.ServiceModel.InstanceContext instanceContext)
        {
            return GetInstance(instanceContext, null);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }
    }

}